<template>
  <div class="app-container">
    <!-- 组件1 -->
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="区域名字" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入省市区名字"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域类型" prop="category">
        <el-select v-model="queryParams.category" placeholder="请选择类型" clearable>
          <el-option
            v-for="dict in area_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:area:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          plain
          icon="Sort"
          @click="toggleExpandAll"
        >展开/折叠</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 组件2 -->
     <AreaTableList @handleUpdate="handleUpdate" :area_type="area_type" :refreshTable="refreshTable" :loading="loading" :areaList="areaList" :isExpandAll="isExpandAll" />


    <!-- 添加或修改地区对话框 -->
    <SaveArea @submitForm="submitForm" @cancelBox="cancel" :formInfo="form" :area_type="area_type" :title="title" :open="open" :areaOptions="areaOptions" />
   
  </div>
</template>

<script setup name="Area">
import { listArea, getArea, delArea, addArea, updateArea } from "@/api/wms/area";
import SaveArea from '@/components/Area/save.vue'
import AreaTableList from '@/components/Area/areaList.vue'

const { proxy } = getCurrentInstance();
const { area_type } = proxy.useDict('area_type');

const areaList = ref([]);
const areaOptions = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const title = ref("");
const isExpandAll = ref(true);
const refreshTable = ref(true);

const data = reactive({
  form: {},
  queryParams: {
    name: null,
    category: null,
    sort: null,
    parentId: null
  },
  rules: {
    name: [
      { required: true, message: "省市区名字不能为空", trigger: "blur" }
    ],
    category: [
      { required: true, message: "类型不能为空", trigger: "change" }
    ],
    sort: [
      { required: true, message: "排序值不能为空", trigger: "blur" }
    ],
    parentId: [
      { required: true, message: "父级区域，0标识无父级区域不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询地区列表 */
function getList() {
  loading.value = true;
  listArea(queryParams.value).then(response => {
    areaList.value = proxy.handleTree(response.data, "id", "parentId");
    loading.value = false;
  });
}

/** 查询地区下拉树结构 */
function getTreeselect() {
  listArea().then(response => {
    areaOptions.value = [];
    const data = { id: 0, name: '顶级节点', children: [] };
    data.children = proxy.handleTree(response.data, "id", "parentId");
    areaOptions.value.push(data);
  });
}
	
// 取消按钮
function cancel() {
  open.value = false;
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    name: null,
    category: null,
    sort: null,
    parentId: null
  };
  proxy.resetForm("areaRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮操作 */
function handleAdd(row) {
  reset();
  getTreeselect();
  if (row != null && row.id) {
    form.value.parentId = row.id;
  } else {
    form.value.parentId = 0;
  }
  open.value = true;
  title.value = "添加地区";
}

/** 展开/折叠操作 */
function toggleExpandAll() {
  refreshTable.value = false;
  isExpandAll.value = !isExpandAll.value;
  nextTick(() => {
    refreshTable.value = true;
  });
}

/** 修改按钮操作 */
async function handleUpdate(row) {
  //查询树
  await getTreeselect();
  if (row != null) {
    form.value.parentId = row.id;
  }
  //查询要修改的数据
  getArea(row.id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改地区";
  });
}

/** 提交按钮 */
function submitForm(dataForm) {
  if (dataForm.id != null) {
      updateArea(dataForm).then(response => {
        proxy.$modal.msgSuccess("修改成功");
        open.value = false;
        getList();
      });
    } else {
      addArea(dataForm).then(response => {
        proxy.$modal.msgSuccess("新增成功");
        open.value = false;
        getList();
      });
    }
}

/** 删除按钮操作 */
function handleDelete(row) {
  proxy.$modal.confirm('是否确认删除地区编号为"' + row.id + '"的数据项？').then(function() {
    return delArea(row.id);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

getList();
</script>
